<template>
  <div class="pakpay">
    <div class="pakpay-form">
      <el-form
        label-position="right"
        label-width="10px"
        style="display: inline-block; display: flex"
      >
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item>
              <el-input
                placeholder="车位名称"
                v-model="form.parkname"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-input placeholder="应缴金额" v-model="form.pakpay"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-select
                style="width: 100%"
                placeholder="缴费状态"
                v-model="form.orderstatus"
              >
                <el-option label="已缴费" value="1"></el-option>
                <el-option label="未缴费" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-button
          type="primary"
          style="margin-left: 20px; width: 9%"
          @click="getTableData"
          ><i class="el-icon-search"></i>查询</el-button
        >
        <el-button type="success" style="width: 9%" @click="fimalyExcel()"
          ><i class="el-icon-document-copy"></i>全量导出</el-button
        >
        <el-button type="primary" style="width: 9%" @click="resetForm()"
          ><i class="el-icon-refresh-right"></i>重置</el-button
        >
      </el-form>
    </div>
    <div class="pakpay-table">
      <el-table :data="tableData" border style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>

        <el-table-column prop="parkname" label="车位名称" width="220">
        </el-table-column>
        <el-table-column prop="parkpay" label="车位金额" width="200">
        </el-table-column>
        <el-table-column prop="startdate" label="开始时间" width="200">
        </el-table-column>
        <el-table-column prop="enddate" label="结束时间" width="200">
        </el-table-column>
        <el-table-column prop="orderpay" label="应邀金额" width="200">
        </el-table-column>
        <el-table-column prop="orderstatus" label="缴费状态" width="200">
          <template slot-scope="scope">
            <el-switch
              disabled
              v-model="scope.row.orderstatus"
              :active-value="2"
              :inactive-value="1"
              @change="handleParkStatusChange(scope.row)"
              >111</el-switch
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" size="small"
              >通知缴费</el-button
            >
            <el-button size="small" @click="handleDetail(scope.row)"
              >缴费详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 通知缴费弹窗 -->
    <el-dialog
      title="车位订单通知"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="tableData1"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="tableData1.startdate"
                type="date"
                placeholder="开始时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="tableData1.enddate"
                type="date"
                placeholder="结束时间"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="应缴金额" prop="parkname">
          <el-input v-model="tableData1.orderpay"></el-input>
        </el-form-item>
        <el-form-item prop="orderstatus" label="缴费状态" width="200">
          <el-select v-model="tableData1.orderstatus" placeholder="请选择状态">
            <el-option label="未缴费" :value="1"></el-option>
            <el-option label="已缴费" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="通知缴费" prop="parkpayNotify">
          <el-input v-model="tableData1.note"> </el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="notice()">通知</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </span>
    </el-dialog>

    <!-- 通知缴费详情 -->
    <el-dialog
      title="车位订单详情"
      :visible.sync="dialogVisible1"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="handselect"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="handselect.startdate"
                disabled
                type="date"
                placeholder="开始时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="handselect.enddate"
                disabled
                type="date"
                placeholder="结束时间"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="应缴金额" prop="parkname">
          <el-input disabled v-model="handselect.orderpay"></el-input>
        </el-form-item>
        <el-form-item prop="orderstatus" label="缴费状态" width="200">
          <el-select
            disabled
            v-model="handselect.orderstatus"
            placeholder="请选择状态"
          >
            <el-option label="未缴费" :value="1"></el-option>
            <el-option label="已缴费" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户回复" prop="parkpayNotify">
          <el-input disabled v-model="handselect.reply"> </el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">关闭</el-button>
      </span>
    </el-dialog>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[100, 200, 300, 400]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { formats } from "@/assets/js/formatDate";
export default {
  data() {
    return {
      value: true,
      total: 0,
      form: {
        parkname: "",
        orderstatus: "",
        orderpay: "",
        pageNum: 10,
        currPage: 0,
      },
      dialogVisible: false,
      dialogVisible1: false,
      tableData: [],
      handselect: [],
      tableData1: "",
    };
  },
  methods: {
    handleSizeChange(val) {
      this.form.pageNum = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.form.currPage = val;
      this.getTableData();
    },
    handleDetail(val) {
      this.dialogVisible1 = true;
      this.handselect = val;
      console.log(val);
    },
    // 缴费通知接口
    async notice() {
      this.dialogVisible = false;

      const params = {
        u_id: this.tableData1.id,
        startdate: formats.formatDate(this.tableData1.startdate), // 格式化开始时间
        enddate: formats.formatDate(this.tableData1.enddate), // 格式化结束时间
        note: this.tableData1.note,
        orderstatus: this.tableData1.orderstatus,
        orderpay: this.tableData1.orderpay,
        token: sessionStorage.token,
      };

      try {
        const { data: res } = await this.axios.post(
          `parking/editParkOrder?u_id=${this.tableData1.id}&startdate=${this.tableData1.startdate}&enddate=${this.tableData1.enddate}&note=${this.tableData1.note}&orderstatus=${this.tableData1.orderstatus}&orderpay=${this.tableData1.orderpay}&token=${sessionStorage.token}`,
          params
        );

        console.log(res);
        this.tableData1 = "";
      } catch (error) {
        this.handleError("获取数据失败", error);
      }
    },
    reset() {
      (this.tableData1.startdate = ""),
        (this.tableData1.enddate = ""),
        (this.tableData1.note = ""),
        (this.tableData1.orderstatus = ""),
        (this.tableData1.orderpay = ""),
        this.getTableData();
    },
    handleClick(val) {
      this.dialogVisible = true;
      console.log(val);
      this.tableData1 = val;
    },
    // 通知缴费弹窗
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done(); // 确认时执行的操作
        })
        .catch(() => {
          done(); // 取消时执行的操作
        });
    },
    async getTableData() {
      try {
        const { data: res } = await this.axios.get("parking/getParkOrder?", {
          params: this.form,
        });
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      } catch (error) {
        this.handleError("获取数据失败", error);
      }
    },
    resetForm() {
      this.form = {
        parkname: "",
        orderstatus: "",
        orderpay: "",
        pageNum: 10,
        currPage: 0,
      };
    },
    // 导出excel
    async fimalyExcel() {
      try {
        const { data: res } = await this.axios.get("parking/getParkOrder", {
          params: this.form, // 使用 params 将表单数据作为查询字符串
          responseType: "blob", // 设置响应类型为 blob
        });

        // 处理文件下载
        const url = URL.createObjectURL(new Blob([res]));
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", "样本审核数据.xlsx"); // 文件名
        document.body.appendChild(link);
        link.click();
        link.remove();

        this.$message.success("导出成功！");
      } catch (error) {
        console.error(error);
        this.$message.error("导出失败: " + error.message);
      }
    },
  },
  created() {
    this.getTableData();
  },
};
</script>

<style lang="scss" scoped>
.block {
  text-align: right;
  margin-bottom: 20px;
}
.pakpay {
  margin: 1.3% 1%;
  padding: 0 0;
  background-color: #fff;
  height: calc(100% - 46px);
  display: flex;
  flex-direction: column;

  .pakpay-form {
    padding: 20px;

    .el-form-item {
      margin-bottom: 0; // 调整表单项之间的间距
    }
  }

  .pakpay-table {
    flex: 2;
    padding: 20px;
  }
}
.el-col-8 {
  width: 335px;
}
::v-deep .el-table td.el-table__cell,
.el-table th.el-table__cell.is-leaf {
  border-bottom: 1px solid #ebeef5;
  text-align: center;
}
::v-deep th.el-table__cell.is-leaf {
  border-bottom: 1px solid #ebeef5;
  text-align: center;
}
::v-deep .el-table .el-table__cell {
  padding: 5px 0;
}
</style>